package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipRepairRepository;
import cc.comac.mom.bean.EquipRepairRepositorySpareParts;
import cc.comac.mom.bean.EquipRepairRepositorySparePartsBean;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipRepairRepositoryDao;
import cc.comac.mom.dao.entity.EquipRepairRepositorySparePartsDao;
import cc.comac.mom.datahandlers.base.DataHandler;
import cc.comac.mom.entity.EquipRepairRepositoryEntity;
import cc.comac.mom.entity.EquipRepairRepositorySparePartsEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class EquipRepairRepositorySparePartsDataHandler 
			extends ServiceImpl<EquipRepairRepositorySparePartsDao,EquipRepairRepositorySparePartsEntity>
			implements DataHandler<EquipRepairRepositorySpareParts>{
	
	@Autowired
	private EquipRepairRepositorySparePartsDao EquipRepairRepositorySparePartsDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	@Autowired
	private EquipRepairRepositoryDao equipRepairRepositoryDao;
	
	@Override
	public ResultBean save(EquipRepairRepositorySpareParts obj) {
		ResultBean resultBean=new ResultBean(false);
		EquipRepairRepositorySparePartsEntity entity = obj.getInnerData();
		
		long equipRepairRepositoryId = entity.getEquipRepairRepositoryId();
		String sparePartsNo = entity.getSparePartsNo();
		
		QueryWrapper<EquipRepairRepositorySparePartsEntity> queryWrapper = new QueryWrapper<EquipRepairRepositorySparePartsEntity>();
		queryWrapper.eq("equip_repair_repository_id", equipRepairRepositoryId);
		queryWrapper.eq("spare_parts_no", sparePartsNo);
		queryWrapper.eq("row_status", 1);
		List<EquipRepairRepositorySparePartsEntity> existsEntity = EquipRepairRepositorySparePartsDao.selectList(queryWrapper);
		
		if(existsEntity!=null && existsEntity.size()>0) {
			throw new RuntimeException("已经存在");
			/*resultBean.setErrorMessge("equip.repair.repository.exists.repairDoc");
			resultBean.setMsgParams(new String[] {sparePartsNo});
			return resultBean;*/
		}
		baseEntityDataUtil.setBaseEntityData(entity);
		boolean save = this.save(entity);
		if(!save) {
			throw new RuntimeException("保存失败");
			/*resultBean.setErrorMessge("equip.repair.repository.saveError");
			resultBean.setMsgParams(new String[] {sparePartsNo});
			return resultBean;*/
		}
		
		resultBean.setSuccess(true);
		return resultBean;
		
	}
	
	@Override
	public ResultBean update(EquipRepairRepositorySpareParts obj) {
		ResultBean resultBean=new ResultBean(false);
		EquipRepairRepositorySparePartsEntity entity = obj.getInnerData();
		
		long id = entity.getId();
		EquipRepairRepositorySparePartsEntity entityById = super.getById(id);
		if(entityById==null) {
			throw new RuntimeException("主数据不存在");
			/*resultBean.setErrorMessge("equip.repair.repository.notExists");
			resultBean.setMsgParams(new String[] {id+""});
			return resultBean;*/
		}
		
		long equipRepairRepositoryId = entity.getEquipRepairRepositoryId();
		String sparePartsNo = entity.getSparePartsNo();
		
		QueryWrapper<EquipRepairRepositorySparePartsEntity> queryWrapper = new QueryWrapper<EquipRepairRepositorySparePartsEntity>();
		queryWrapper.eq("equip_repair_repository_id", equipRepairRepositoryId);
		queryWrapper.eq("spare_parts_no", sparePartsNo);
		queryWrapper.ne("equip_repair_repository_spare_parts_id", id);
		queryWrapper.eq("row_status", 1);
		List<EquipRepairRepositorySparePartsEntity> existsEntity = EquipRepairRepositorySparePartsDao.selectList(queryWrapper);
		
		if(existsEntity!=null && existsEntity.size()>0) {
			throw new RuntimeException("已经存在");
			/*resultBean.setErrorMessge("equip.repair.repository.exists.repairSpareParts");
			resultBean.setMsgParams(new String[] {sparePartsNo});
			return resultBean;*/
		}
		baseEntityDataUtil.updateBaseEntityData(entity);
		boolean save = this.updateById(entity);
		if(!save) {
			throw new RuntimeException("保存失败");
			/*resultBean.setErrorMessge("equip.repair.repository.saveError");
			resultBean.setMsgParams(new String[] {sparePartsNo});
			return resultBean;*/
		}
		
		resultBean.setSuccess(true);
		return resultBean;
		
	}

	@Override
	public ResultBean delete(EquipRepairRepositorySpareParts obj) {
		ResultBean resultBean=new ResultBean(false);
		EquipRepairRepositorySparePartsEntity entity = obj.getInnerData();
		
		long id = entity.getId();
		EquipRepairRepositorySparePartsEntity existsEntity = EquipRepairRepositorySparePartsDao.selectById(id);
		if(existsEntity==null) {
			throw new RuntimeException("主数据不存在");
			/*resultBean.setErrorMessge("equip.repair.repository.notExists");
			resultBean.setMsgParams(new String[] {id+""});
			return resultBean;*/
		}
		baseEntityDataUtil.updateBaseEntityData(entity);
		entity.setRowStatus(0);
		boolean save = this.updateById(entity);
		if(!save) {
			throw new RuntimeException("保存失败");
			/*resultBean.setErrorMessge("equip.repair.repository.saveError");
			resultBean.setMsgParams(new String[] {entity.getSparePartsNo()});
			return resultBean;*/
		}
		
		resultBean.setSuccess(true);
		return resultBean;
	}

	@Override
	public ResultBean save(List<EquipRepairRepositorySpareParts> objs) {
		ResultBean resultBean=new ResultBean(false);
		List<EquipRepairRepositorySparePartsEntity> entityList = new ArrayList<>();
		
		for(EquipRepairRepositorySpareParts obj:objs) {
			EquipRepairRepositorySparePartsEntity entity = obj.getInnerData();
			
			long equipRepairRepositoryId = entity.getEquipRepairRepositoryId();
			String sparePartsNo = entity.getSparePartsNo();
			
			QueryWrapper<EquipRepairRepositorySparePartsEntity> queryWrapper = new QueryWrapper<EquipRepairRepositorySparePartsEntity>();
			queryWrapper.eq("equip_repair_repository_id", equipRepairRepositoryId);
			queryWrapper.eq("spare_parts_no", sparePartsNo);
			queryWrapper.eq("equip_part_id",entity.getEquipPartId());
			queryWrapper.eq("row_status", 1);
			List<EquipRepairRepositorySparePartsEntity> existsEntity = EquipRepairRepositorySparePartsDao.selectList(queryWrapper);
			
			if(existsEntity!=null && existsEntity.size()>0) {
				throw new RuntimeException("已经存在");
			}
			baseEntityDataUtil.setBaseEntityData(entity);
			entityList.add(entity);
		}
		
		
		boolean save = this.saveBatch(entityList);
		if(!save) {
			throw new RuntimeException("保存失败");
			/*resultBean.setErrorMessge("equip.repair.repository.saveError");
			return resultBean;*/
		}
		
		resultBean.setSuccess(true);
		return resultBean;
	}

	@Override
	public ResultBean update(List<EquipRepairRepositorySpareParts> objs) {
		ResultBean resultBean=new ResultBean(false);
		List<EquipRepairRepositorySparePartsEntity> entityList = new ArrayList<>();
		for(EquipRepairRepositorySpareParts obj:objs) {
			EquipRepairRepositorySparePartsEntity entity = obj.getInnerData();
			
			long id = entity.getId();
			EquipRepairRepositorySparePartsEntity entityById = super.getById(id);
			if(entityById==null) {
				throw new RuntimeException("主数据不存在");
				/*resultBean.setErrorMessge("equip.repair.repository.notExists");
				resultBean.setMsgParams(new String[] {id+""});
				return resultBean;*/
			}
			
			long equipRepairRepositoryId = entity.getEquipRepairRepositoryId();
			String sparePartsNo = entity.getSparePartsNo();
			
			QueryWrapper<EquipRepairRepositorySparePartsEntity> queryWrapper = new QueryWrapper<EquipRepairRepositorySparePartsEntity>();
			queryWrapper.eq("equip_repair_repository_id", equipRepairRepositoryId);
			queryWrapper.eq("spare_parts_no", sparePartsNo);
			queryWrapper.ne("equip_repair_repository_spare_parts_id", id);
			queryWrapper.eq("row_status", 1);
			List<EquipRepairRepositorySparePartsEntity> existsEntity = EquipRepairRepositorySparePartsDao.selectList(queryWrapper);
			
			if(existsEntity!=null && existsEntity.size()>0) {
				throw new RuntimeException("已经存在");
				/*resultBean.setErrorMessge("equip.repair.repository.exists.repairSpareParts");
				resultBean.setMsgParams(new String[] {sparePartsNo});
				return resultBean;*/
			}
			baseEntityDataUtil.updateBaseEntityData(entity);
			entityList.add(entity);
		}
		
		boolean save = this.updateBatchById(entityList);
		if(!save) {
			throw new RuntimeException("保存失败");
			/*resultBean.setErrorMessge("equip.repair.repository.saveError");
			return resultBean;*/
		}
		resultBean.setSuccess(true);
		return resultBean;
	}

	@Override
	public ResultBean delete(List<EquipRepairRepositorySpareParts> objs) {
		ResultBean resultBean=new ResultBean(false);
		List<EquipRepairRepositorySparePartsEntity> entityList = new ArrayList<>();
		for(EquipRepairRepositorySpareParts obj:objs) {
			EquipRepairRepositorySparePartsEntity entity = obj.getInnerData();
			long id = entity.getId();
			EquipRepairRepositorySparePartsEntity existsEntity = EquipRepairRepositorySparePartsDao.selectById(id);
			if(existsEntity==null) {
				throw new RuntimeException("数据不存在");
				/*resultBean.setErrorMessge("equip.repair.repository.notExists");
				resultBean.setMsgParams(new String[] {entity.getSparePartsNo()});
				return resultBean;*/
			}
			baseEntityDataUtil.updateBaseEntityData(entity);
			entity.setRowStatus(0);
			entityList.add(entity);
		}
		
		boolean save = this.updateBatchById(entityList);
		if(!save) {
			throw new RuntimeException("保存失败");
			/*resultBean.setErrorMessge("equip.repair.repository.saveError");
			return resultBean;*/
		}
		
		resultBean.setSuccess(true);
		return resultBean;
	}

	@Override
	public EquipRepairRepositorySpareParts getById(long id) {
		EquipRepairRepositorySparePartsEntity entity = super.getById(id);
		if(entity!=null) {
			EquipRepairRepositorySpareParts bean = new EquipRepairRepositorySpareParts();
			bean.setInnerData(entity);
			return bean;
		}
		return null;
	}

	public ResultDev selectByCondition(EquipRepairRepository obj) {
		ResultDev resultDev = new ResultDev();
    	QueryWrapper<EquipRepairRepositoryEntity> queryWrapper = new QueryWrapper<EquipRepairRepositoryEntity>();
    	queryWrapper.eq("row_status",1);
    	queryWrapper.eq("equip_category_id",obj.getInnerData().getEquipCategoryId());
    	queryWrapper.eq("fault_cause_id",obj.getInnerData().getFaultCauseId());
    	
    	List<EquipRepairRepositoryEntity> listEquipRepairRepositoryEntity = equipRepairRepositoryDao.selectList(queryWrapper);

    	if(listEquipRepairRepositoryEntity == null || listEquipRepairRepositoryEntity.size()<1) {
    		return null;
    	}
    	EquipRepairRepositoryEntity equipRepairRepositoryEntity = listEquipRepairRepositoryEntity.get(0);
    	QueryWrapper<EquipRepairRepositorySparePartsEntity> queryWrapper1 = new QueryWrapper<EquipRepairRepositorySparePartsEntity>();
    	queryWrapper1.eq("row_status",1);
    	queryWrapper1.eq("equip_repair_repository_id",equipRepairRepositoryEntity.getId());
    	
        Page<EquipRepairRepositorySparePartsEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
        IPage<EquipRepairRepositorySparePartsEntity> iPage = EquipRepairRepositorySparePartsDao.selectPage(page, queryWrapper1);
        List<EquipRepairRepositorySparePartsEntity> list = iPage.getRecords();
        
		resultDev.setEntityList(list);
		resultDev.setTotalQuantity((int)iPage.getTotal());
		resultDev.setPageCount((int)iPage.getPages());
		return resultDev;
		
	}
	
	public ResultDev selectByFields(EquipRepairRepositorySparePartsBean obj) {
		ResultDev resultDev = new ResultDev();
    	QueryWrapper<EquipRepairRepositoryEntity> queryWrapper = new QueryWrapper<EquipRepairRepositoryEntity>();
    	queryWrapper.eq("row_status",1);
    	queryWrapper.eq("equip_category_id",obj.getEquipCategoryId());
    	queryWrapper.eq("fault_cause_id",obj.getFaultCauseId());
    	
    	List<EquipRepairRepositoryEntity> listEquipRepairRepositoryEntity = equipRepairRepositoryDao.selectList(queryWrapper);

    	if(listEquipRepairRepositoryEntity == null || listEquipRepairRepositoryEntity.size()<1) {
    		return null;
    	}
    	obj.setEquipRepairRepositoryId(listEquipRepairRepositoryEntity.get(0).getId());
		if (obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		obj.setPageNum((obj.getPageNum()-1)*obj.getPageSize());
		List<EquipRepairRepositorySparePartsBean> listEquipPartBorrowBean = EquipRepairRepositorySparePartsDao.selectByFields(obj);
		long countByFields = EquipRepairRepositorySparePartsDao.selectCountByFields(obj);
		
		// 将查询总数除以每页数量后向上取整获取总页数
		double pageCount = Math.ceil((double)countByFields / obj.getPageSize());
		resultDev.setEntityList(listEquipPartBorrowBean);
		resultDev.setPageCount((int)pageCount);
		resultDev.setTotalQuantity((int)countByFields);
		return resultDev;
	}
}
